package com.ruoyi.system.outto.goods.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.goods.NEWTHGAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class NEWTHGAPIImpl implements NEWTHGAPI {

    @Resource
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    InterAttrMapper interAttrMapper;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "add", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm("https://api.taohaoge.com/interfaces/receiveProduct.ashx", params, null);
        log.info("淘号阁添加接口请求参数={}", JSONObject.toJSONString(params));
        log.info("淘号阁添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
                result.put("outId", String.valueOf(goodsInfoAndAttrValueDto.getId()));
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "添加失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "update", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm("https://api.taohaoge.com/interfaces/receiveProduct.ashx", params, null);
        log.info("淘号阁编辑接口请求参数={}", JSONObject.toJSONString(params));
        log.info("淘号阁编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "编辑失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }

    private void builderServer(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map params, String server) {
        //========================开始匹配区服属性=====================//
        JSONObject jsonObject = JSONObject.parseObject(server);
        //开始匹配一级区服
        String interServeReqs = jsonObject.getString("interServeReqs");
        String qufuid = "";
        JSONObject oneServerJson = new JSONObject();
        if (Objects.equals(goodsInfoAndAttrValueDto.getGameType(), "0")) {
            qufuid = goodsInfoAndAttrValueDto.getSystemId() + "" + goodsInfoAndAttrValueDto.getCarrierId() + "";
            List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);
            for (JSONObject object : oneServer) {
                String ydServeId = object.getString("YdServeId");
                if (StringUtils.isNotEmpty(ydServeId)) {
                    ydServeId = ydServeId.replace(",", "").replace(" ", "");
                    if (qufuid.contains(ydServeId)) {
                        oneServerJson = object;
                        break;
                    }
                }
            }
        } else {
            if(goodsInfoAndAttrValueDto.getGameName().equals("无畏契约")||goodsInfoAndAttrValueDto.getGameName().equals("穿越火线")||goodsInfoAndAttrValueDto.getGameName().equals("永劫无间")){
                qufuid = goodsInfoAndAttrValueDto.getGroupId() + "" + goodsInfoAndAttrValueDto.getServerId() + "";
                List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);
                for (JSONObject object : oneServer) {
                    String ydServeId = object.getString("YdServeId");
                    if (StringUtils.isNotEmpty(ydServeId)) {
                        ydServeId = ydServeId.replace(",", "").replace(" ", "");
                        if (qufuid.contains(ydServeId)) {
                            oneServerJson = object;
                            break;
                        }
                    }
                }
            }else if(goodsInfoAndAttrValueDto.getGameName().equals("地下城与勇士")){
                qufuid = goodsInfoAndAttrValueDto.getGroupId() + "";
                List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);
                for (JSONObject object : oneServer) {
                    String interServerRes = object.getString("interServerRes");
                    List<JSONObject> resServer = JSONObject.parseArray(interServerRes, JSONObject.class);
                    for(JSONObject resobject : resServer) {
                        String ydServeId = resobject.getString("YdServeId");
                        if (StringUtils.isNotEmpty(ydServeId)) {
                            ydServeId = ydServeId.replace(",", "").replace(" ", "");
                            if (qufuid.contains(ydServeId)) {
                                oneServerJson = resobject;
                                break;
                            }
                        }
                    }
                }
            } else {
                qufuid = goodsInfoAndAttrValueDto.getServerId() + "";
                List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);
                for (JSONObject object : oneServer) {
                    String ydServeId = object.getString("YdServeId");
                    if (StringUtils.isNotEmpty(ydServeId)) {
                        String interServerRes = object.getString("interServerRes");
                        List<JSONObject> oneServers = JSONObject.parseArray(interServerRes, JSONObject.class);
                        for (JSONObject objects : oneServers) {
                            ydServeId = objects.getString("YdServeId");
                            if(StringUtils.isNotEmpty(ydServeId)) {
                                ydServeId = ydServeId.replace(",", "").replace(" ", "");
                                if (Objects.equals(qufuid, ydServeId)) {
                                    oneServerJson = objects;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (Objects.isNull(oneServerJson)) {
            throw new GlobalException("运营商未匹配到");
        }
        params.put("serverid", oneServerJson.getString("InterServeId"));
    }

    /**
     * 开始匹配单选属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param radioAttr
     */
    private String builderRadioAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String radioAttr, String optionss) {
        //========================开始匹配单选属性=====================//
        JSONObject jsonObject = JSONObject.parseObject(radioAttr);
        String interRadioReqs = jsonObject.getString("interRadioReqs");
        log.info(JSONObject.toJSONString(interRadioReqs));
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        if (!org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            /**
             * 1、先查询自定义中所有单选的字段
             * 2、再查该账号单选字段中的选项
             * 3、再去接口属性匹配表过滤
             */
            GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
            gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
            gameAttrTemplate.setIsEnable("0");
            gameAttrTemplate.setFieldType("radio");
            List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
            Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();


            List<JSONObject> defObjects = new ArrayList<>();

            defObjects.addAll(jsonObjects);
            for (JSONObject object : jsonObjects) {
                for (GameAttrTemplate attrTemplate : gameAttrTemplates) {
                    List<Object> list = new ArrayList<>();
                    String fieldName = "";
                    String aavName = attrTemplate.getAavName();
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (aavName.equals(name)) {
                            //一级属性
                            //注意 YdConfigId 对应的是 f字段对应的 id
                            if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                                String InterConfigId = object.getString("InterConfigId");
                                defObjects.remove(object);//如果匹配上，则去掉
                                String interRadioReqs1 = object.getString("interRadioReqs");
                                List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                                declaredField.setAccessible(true);
                                try {
                                    //获取选择的值 例如  实名中的可二次
                                    Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                    String oneValue = String.valueOf(o);
                                    List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> Objects.equals(j.getString("YdConfigId"), oneValue)).collect(Collectors.toList());
                                    String interConfigName = object.getString("InterConfigName");
                                    String defaultValue = object.getString("defaultValue");
                                    if (org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                        if (StringUtils.isNotEmpty(defaultValue)) {
//                                        mateMap.put("id", InterConfigId);
//                                        mateMap.put("val", defaultValue);
                                            fieldName = InterConfigId;
                                            list.add(defaultValue);
                                        }
                                    }
//                                if (org.springframework.util.CollectionUtils.isEmpty(twoAttr) && StringUtils.isEmpty(defaultValue)) {
//                                    throw new GlobalException("属性" + interConfigName + " 接口要求必传，但是暂未匹配到，请设置默认值");
//                                }
                                    if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                        JSONObject jsonObject2 = twoAttr.get(0);
                                        String interConfigId2 = jsonObject2.getString("name");
                                        fieldName = InterConfigId;
                                        list.add(interConfigId2);
                                    }
                                } catch (Exception e) {
                                    throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                                }
                            }
                        }
                    }
                    if (StringUtils.isNotEmpty(list)) {
                        String join = StringUtils.join(list, ",");
                        String xingyuanzhencang = fieldName+"=" + join;
                        optionss += "^123|^" + xingyuanzhencang;
                    }
                }
            }
            //开始匹配默认值
            for (JSONObject object : defObjects) {
                List<Object> list = new ArrayList<>();
                String fieldName = object.getString("defaultValue");
                interRadioReqs = object.getString("interRadioReqs");
                jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
                for (JSONObject objects : jsonObjects) {
                    if(StringUtils.isNotEmpty(objects.getString("defaultValue"))){
                        list.add(objects.getString("defaultValue"));
                    }
                }
                if (StringUtils.isNotEmpty(list)) {
                    String join = StringUtils.join(list, ",");
                    String xingyuanzhencang = fieldName+"=" + join;
                    optionss += "^123|^" + xingyuanzhencang;
                }
            }
        }
        return optionss;
        //========================单选属性匹配结束=====================//
    }
    private String builderCheckAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String checkAttr, String optionss) {
        JSONObject checkOtherJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkOtherJson.getString("interCheckBoxReqs");
        Map<String, List<JSONObject>> otherMap = new HashMap<>();
        if (!StringUtils.isEmpty(interCheckBoxReqs)) {
            List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
            checkJsonList = checkJsonList.stream().filter(s -> StringUtils.isNotEmpty(s.getString("YdConfigId"))).collect(Collectors.toList());
            otherMap = checkJsonList.stream().collect(Collectors.groupingBy(s -> s.getString("YdConfigId")));

        }
        /**
         * 1、先查询自定义中所有多选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        if (org.springframework.util.CollectionUtils.isEmpty(checkAttrJson)) {
            throw new GlobalException("接口多选属性暂未配置");
        }

        List<String> paramList = new ArrayList<>();

        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        List<String> types = new ArrayList<>();
        types.add("checkbox");
        types.add("boxradio");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        for (GameAttrTemplate attrTemplate : checkAttrTemplates) {
            //属性配置F字段Key
            String aavName = attrTemplate.getAavName();
            for (Field declaredField : declaredFields) {
                String name = declaredField.getName();
                if (aavName.equals(name)) {
                    declaredField.setAccessible(true);
                    Object o = null;
                    try {
                        o = declaredField.get(goodsInfoAndAttrValueDto);
                    } catch (IllegalAccessException e) {
                        throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                    }
                    String oneValue = String.valueOf(o);
                    if (StringUtils.isNotEmpty(oneValue)) {
                        List<String> list = Arrays.asList(oneValue.split(","));
                        paramList.addAll(list);
                    }
                }
            }
        }
        if (CollectionUtils.isEmpty(paramList)) {
            paramList = new ArrayList<>(new HashSet<>(paramList));
        }
        // 整合虞都有的属性
//        List<JSONObject> dataList = new ArrayList<>();
//        for (String str : paramList) {
//            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(otherMap.get(str))) {
//                JSONObject json = otherMap.get(str).get(0);
//                dataList.add(json);
//            }
//        }
//        Map<String, List<JSONObject>> collect = dataList.stream().collect(Collectors.groupingBy(s -> s.getString("id")));

        //获取线上属性
        List<JSONObject> list = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        if (!CollectionUtils.isEmpty(list)) {
            for (JSONObject jsonObject : list) {
                String fieldName = jsonObject.getString("id");
                List<Object> list2 = new ArrayList<>();
                List<JSONObject> interServerRes = JSONObject.parseArray(jsonObject.getString("interServerRes"), JSONObject.class);
                if (!CollectionUtils.isEmpty(interServerRes)) {
                    for (JSONObject interServerRe : interServerRes) {
                        if (paramList.contains(interServerRe.getString("YdConfigId"))) {
//                            JSONObject object = interServerRe;
                            list2.add(interServerRe.getString("id"));
                        }
                    }
                    if (!CollectionUtils.isEmpty(list2)) {
                        String join = StringUtils.join(list2, ",");
                        String xingyuanzhencang = fieldName+"=" + join;
                        optionss += "^123|^" + xingyuanzhencang;
                    }
                }
            }
        }
//        for (JSONObject object : checkJsonList) {
//            for (GameAttrTemplate attrTemplate : checkAttrTemplates) {
//                List<Object> list = new ArrayList<>();
//                String fieldName = "";
//                String aavName = attrTemplate.getAavName();
//                for (Field declaredField : declaredFields) {
//                    String name = declaredField.getName();
//                    if (aavName.equals(name)) {
//                        declaredField.setAccessible(true);
//
////                        List<String> list2 = new ArrayList<>();
////                        for (String s : split2) {
////                            s = s.replace(" ", "");
////                            list2.add(s);
////                        }
//                        Object o = null;
//                        try {
//                            o = declaredField.get(goodsInfoAndAttrValueDto);
//                        } catch (IllegalAccessException e) {
//                            throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
//                        }
//                        String oneValue = String.valueOf(o);
//                        String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
//                        //开始匹配二级属性
//                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
//                        /**
//                         * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
//                         */
//                        String pf_ids = "";
//                        for (JSONObject jsonObject1 : jsonObjects2) {
//                            String[] split1 = oneValue.split(",");
//                            String ydConfigId1 = jsonObject1.getString("YdConfigId");
//                            for (String s : split1) {
//                                if (Objects.equals(s, ydConfigId1)) {
////                                    String InterConfigId2 = jsonObject1.getString("InterConfigId");
////                                    pf_ids+=","+InterConfigId2;
//                                    fieldName = object.getString("InterConfigId");
//                                    list.add(jsonObject1.getString("InterConfigId"));
//                                }
//                            }
//                        }
//                    }
//                }
//                if (StringUtils.isNotEmpty(list)) {
//                    String join = StringUtils.join(list, ",");
//                    String xingyuanzhencang = fieldName+"=" + join;
//                    optionss += "^123|^" + xingyuanzhencang;
//                }
//            }
//        }
        return optionss;
    }

    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String type, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("key", salePartner.getSaleKey());
        params.put("merchant_no", salePartner.getTwoOther());
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        params.put("pid", CollectionUtils.isNotEmpty(artRecs) ? artRecs.get(0).getOutid() : goodsInfoAndAttrValueDto.getId());
        params.put("title", goodsInfoAndAttrValueDto.getTitle());
        params.put("content", goodsInfoAndAttrValueDto.getDes());
        params.put("price", goodsInfoAndAttrValueDto.getPrice());

        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        String pic = infoImgs.stream().map(info -> info.getImgUrl()).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
        if (Objects.nonNull(goodsInfoAndAttrValueDto.getTitleImg())) {
            pic = goodsInfoAndAttrValueDto.getTitleImg() + ","+pic;
        }
        params.put("pic", pic);
        params.put("paiwei", "可排位");
        params.put("account", goodsInfoAndAttrValueDto.getAccountNumber());
        params.put("type", type);

        //自动匹配开始
        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_")+1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        params.put("gameid",interAttr.getInterGameId());
        //区服
        builderServer(goodsInfoAndAttrValueDto, params, interAttr.getServer());

        String optionss = "";
        //开始匹配单选属性
        optionss = builderRadioAttr(goodsInfoAndAttrValueDto, interAttr.getRadioAttr(),optionss);
        //开始匹配多选属性
        optionss = builderCheckAttr(goodsInfoAndAttrValueDto, interAttr.getCheckAttr(),optionss);
        if(StringUtils.isNotEmpty(optionss)) {
            params.put("options", optionss.substring(6));
        }
        GameAttribute gameAttribute = null;
        if ("王者荣耀".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF16())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
        }
        else if ("和平精英".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }
        else if ("穿越火线".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF3())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
        }
        else if ("枪战王者".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("逆战")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("光遇")) {

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("英雄联盟")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("原神")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF20())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("火影忍者")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF7())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF7()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("蛋仔派对")) {
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("英雄联盟(手游)")){
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF4())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF4()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("无畏契约")){
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("地下城与勇士")){
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("永劫无间")){

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("暗区突围")){

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("金铲铲之战")){

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("永劫无间手游")){

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("三角洲行动")){

        }
        else if (goodsInfoAndAttrValueDto.getGameName().equals("使命召唤手游")){
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF3())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
        }
        else {
            throw new GlobalException("暂不支持此游戏:" + goodsInfoAndAttrValueDto.getGameName());
        }
        if ("王者荣耀,和平精英".contains(goodsInfoAndAttrValueDto.getGameName())) {
            if (gameAttribute.getFieldName().equals("包人脸")) {
                params.put("smstate", 2);
            } else if (gameAttribute.getFieldName().equals("可二次实名")) {
                params.put("smstate", 1);
            } else {
                params.put("smstate", 1);
            }
        } else if("穿越火线,".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("无修改机会且实名是本人")){
                params.put("smstate", 2);
            }else if(gameAttribute.getFieldName().equals("有修改机会")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 1);
            }
        } else if("英雄联盟,原神,枪战王者".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("无修改人脸机会/实名是本人")){
                params.put("smstate", 2);
            }else if(gameAttribute.getFieldName().equals("有修改人脸机会")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 1);
            }
        } else if("火影忍者".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("人脸包赔")){
                params.put("smstate", 2);
            }else if(gameAttribute.getFieldName().equals("可改二次")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 1);
            }
        } else if("无畏契约".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("支持人脸包赔")){
                params.put("smstate", 2);
            }else if(gameAttribute.getFieldName().equals("可二次实名")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 1);
            }
        }else if("地下城与勇士".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("包人脸")){
                params.put("smstate", 2);
            }else if(gameAttribute.getFieldName().equals("可改实名")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 1);
            }
        }else if("使命召唤手游".contains(goodsInfoAndAttrValueDto.getGameName())){
            if(gameAttribute.getFieldName().equals("可改实名")){
                params.put("smstate", 1);
            }else {
                params.put("smstate", 2);
            }
        }else {
            params.put("smstate", 1);
        }
        return params;
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        List<GameAttribute> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(ids)) {
            list = gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                    Long.class));
        }
        return list;
    }

    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "delete", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpRequest.post("https://api.taohaoge.com/interfaces/receiveProduct.ashx").form(params).execute().body();
        log.info("淘号阁删除接口请求参数={}", JSONObject.toJSONString(params));
        log.info("淘号阁删除接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        ArtRec artRec = artRecs.get(0);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "删除失败");
        }
        artRecMapper.updateArtRec(artRec);
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);

        return AjaxResult.success(result);
    }

    public static void main(String[] args) {

        /*JSONObject resultJson1 = JSONObject.parseObject(response);
        log.info(resultJson1);
        JSONArray array = JSONArray.parseArray(resultJson1.get("data").toString());
        for(Object o : array){
            log.info(JSONObject.parseObject(o.toString()).get("propertyname"));
        }*/
        /*JSONObject resultJson1 = JSONObject.parseObject(response);
        JSONArray jsonArray = JSONArray.parseArray(resultJson1.getString("arealist"));
        for (Object o : jsonArray){
            log.info(o);
        }*/
//        String response = games();
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("JDKRUEIUREN89340JIEJRJIERASD");
        salePartner.setOther("http://api.taohaoge.com/interfaces");
        salePartner.setTwoOther("zuyoule");
        String response = youxiqufu(1262,salePartner);

        log.info(response);
    }

    public static String kuozhanshuxing(int type, SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", salePartner.getSaleKey());
        params1.put("merchant_no", salePartner.getTwoOther());
        params1.put("gameid", type);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(salePartner.getOther() + "/getExpendAttribute.ashx", params1, head);
        if(StringUtils.isEmpty(response)){
            throw new GlobalException("获取淘号阁属性失败!!!");
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if(!"true".equals(resultJson.getString("succ"))){
            throw new GlobalException(response);
        }
        return response;
    }

    public static String youxiqufu(int type, SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", salePartner.getSaleKey());
        params1.put("merchant_no", salePartner.getTwoOther());
        params1.put("gameid", type);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String URL = salePartner.getOther() + "/getAreaServer.ashx";
        String response = HttpUtils.doPostForm(URL, params1, head);
        if(StringUtils.isEmpty(response)){
            throw new GlobalException("获取淘号阁区服失败!!!");
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if(!"true".equals(resultJson.getString("succ"))){
            throw new GlobalException(response);
        }
        return response;
    }

    public static String games(SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", salePartner.getSaleKey());
        params1.put("merchant_no", salePartner.getTwoOther());
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(salePartner.getOther() + "/getGameInfor.ashx", params1, head);
        if(StringUtils.isEmpty(response)){
            throw new GlobalException("获取淘号阁游戏失败!!!");
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if(!"true".equals(resultJson.getString("succ"))){
            throw new GlobalException(response);
        }
        return response;
    }

    public static String getGameID(String gameName, SalePartner salePartner) {
        String response = games(salePartner);
        String data = JSONObject.parseObject(response).getString("data");
        return null;
    }

}
